package cn.uniqueinfo.dp.util;

import cn.uniqueinfo.dp.vo.*;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.List;

/**
 * @author:朱元勋
 * @mail:395910063@qq.com
 * @date:2019-01-17 10:02:24
 * @description: 数据库操作辅助类
 */
@Slf4j
public class DBUtil {

    // 完全公开
    private static String create_auth_all           = "all";
    // 创建人本人
    private static String create_auth_user          = "user";
    // 创建人所在部门
    private static String create_auth_dept          = "dept";
    // 管辖部门包括所在部门
    private static String create_auth_chargedept    = "chargedept";


    public static HashMap genDynaminSql(FormVO formVO, List<FormListVO> fields) {
        HashMap hashMap = new HashMap();
        // hashMap.put("formSql",formVO.getFormSql());
        return hashMap;
    }

    public static HashMap genDynaminSql(FormGridVO formGrid, List<FormGridColumnVO> columnList, List<QueryVO> queryList,List<FormAuthVO> auths,UserVO user) {
        HashMap<String,Object> hashMap = new HashMap<>();
        String sql = "select * from ( " + formGrid.getFormSql() + " ) uqbiz where 1=1 ";

        // 条件查询语句拼接
        if (queryList != null && queryList.size() != 0) {
            StringBuffer whereSb = new StringBuffer();
            for (QueryVO queryVO : queryList) {
                if ("like".equalsIgnoreCase(queryVO.getOpt())) {
                    whereSb.append(" and " + queryVO.getField() + " " + queryVO.getOpt() + " '" + queryVO.getValue() + "%' ");
                } else {
                    whereSb.append(" and " + queryVO.getField() + " " + queryVO.getOpt() + " '" + queryVO.getValue() + "' ");
                }
            }
            sql = sql + whereSb.toString();
        }

        // 数据权限控制语句拼接
        if (auths != null && auths.size() != 0) {
            StringBuffer authSB = new StringBuffer();
            for(FormAuthVO formAuth : auths){
                if(create_auth_all.equalsIgnoreCase(formAuth.getCreateAuth())){

                }else if(create_auth_user.equalsIgnoreCase(formAuth.getCreateAuth())){
                    authSB.append(" and create_user_id = "+user.getId());
                }else if(create_auth_dept.equalsIgnoreCase(formAuth.getCreateAuth())){
                    authSB.append(" and create_dept_id = "+user.getDeptId());
                }else if(create_auth_chargedept.equalsIgnoreCase(formAuth.getCreateAuth())){
                    authSB.append(" and create_dept_id in (select id from sys_dept where instr(path,F_GET_DEPT_PATH("+user.getDeptId()+")) >= 1) ");
                }
            }


            sql = sql + authSB.toString();
        }

        hashMap.put("formSql", sql);
        log.warn("sql:" + sql);
        return hashMap;
    }

    public static void main(String[] args) {
        FormGridVO formGridVO = new FormGridVO();
        formGridVO.setFormSql("select * from biz_demo");
        String query = "[{\"field\":\"code\",\"opt\":\"like\",\"value\":\"中\"},{\"field\":\"dnum\",\"opt\":\"=\",\"value\":\"1\"},{\"field\":\"create_time\",\"opt\":\">=\",\"value\":\"2019-01-15\"},{\"field\":\"create_time\",\"opt\":\"<=\",\"value\":\"2019-01-28\"}]";
        List<QueryVO> queryList = JSONObject.parseArray(query, QueryVO.class);
        //genDynaminSql(formGridVO, null, queryList);
    }
}
